﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace dbPoll_Application.Models
{
    public enum QuestionType
    {
        None = 0,
        Custom = 1,
        Ranking = 2,
        Demographic = 3,
        Numeric = 4,
        Alphanumeric = 5
    }
    public abstract class Question
    {
        public virtual int questionID { get; set; }
        [Required(ErrorMessage = "Question cannot be blank")]
        public virtual String question { get; set; }
        public virtual DateTime creationTime { get; set; }
        public virtual DateTime? deletionTime { get; set; }
        public virtual int questionNumber { get; set; }
        public virtual QuestionType type { get; set; }
        public virtual String fontType { get; set; }
        public virtual String fontColour { get; set; }
        public virtual int fontSize { get; set; }
        public virtual ICollection<Feedback> feedback { get; set; }
        public virtual CountdownTimer countdownTimer { get; set; }
        public virtual ResponseCounter responseCounter { get; set; }
        public virtual CorrectAnswerIndicator correctAnswerIndicator { get; set; }
        public virtual ResponseGrid responseGrid { get; set; }
        public virtual QuestionChart resultsChart { get; set; }
        public virtual QuestionChart comparisonChart { get; set; }
        public virtual Boolean asynchronous { get; set; }
        public virtual ICollection<Participant> responded { get; set; }

        public virtual String GetParentType()
        {
            if (this.type == QuestionType.Numeric || this.type == QuestionType.Alphanumeric)
            {
                return "SAQ";
            }
            return "MCQ";
        }

        public abstract String GetKeypadResponseType();

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            var question = obj as Question;
            if ((System.Object)question == null)
            {
                return false;
            }
            return this.questionID == question.questionID;
        }

        public override int GetHashCode()
        {
            return this.questionID;
        }

        // Only call this on MCQ questions
        public virtual IList<String> getCorrect() 
        {
            MultipleChoiceQuestion multipleQuestion = QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(this.questionID);
            IList<String> correctOptions = new List<String>();

            if (multipleQuestion != null)
            {
                foreach (var option in multipleQuestion.options)
                {
                    // If the answer is correct, store it
                    if (option.correctCurrent)
                    {
                        correctOptions.Add(option.answer);
                    }
                }
            }
            return correctOptions;
        }
    }
}
